package cn.bluemobi.app.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache.ValueWrapper;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.bluemobi.app.entity.output.BankCard;
import cn.bluemobi.app.entity.output.Member;
import cn.bluemobi.app.entity.output.Team;
import cn.bluemobi.app.entity.output.Withdrawals;
import cn.bluemobi.app.mapper.MemberMapper;
import cn.bluemobi.app.service.MemberService;
import cn.bluemobi.common.core.dto.Dto;
import cn.bluemobi.common.core.dto.ListDto;
import cn.bluemobi.common.utils.MD5Tools;

@Service
@Transactional
public class MemberServiceImpl implements MemberService {

	@Autowired
	private MemberMapper memberMapper;
	@Autowired
	private EhCacheCacheManager cacheManager;

	public Member getMemberInfo(Map<String, Object> params) {
		return memberMapper.getMemberInfo(params);
	}

	@Override
	public Dto<Member> register(String cellphone, String password,
			String verifyCode, String inviteCode) {
		Dto<Member> dto = new Dto<Member>();
		if (memberMapper.checkIsExistCellphone(cellphone) > 0) {
			dto.errorMsg("该手机号已注册,请重新输入");
			return dto;
		}
		ValueWrapper vc = cacheManager.getCache("verifyCodeCache").get(
				cellphone + "regVerifyCode");
		if (vc == null || !StringUtils.equals(vc.get().toString(), verifyCode)) {
			dto.errorMsg("您的验证码已过期或不正确，请重新获取");
			return dto;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("cellphone", cellphone);
		params.put("password", MD5Tools.encode(password));
		params.put("inviteCode", cellphone);
		params.put("nickName", cellphone);
		params.put("parentInviteCode", inviteCode);
		memberMapper.register(params);
		if (StringUtils.isEmpty(inviteCode)) {
			memberMapper.updateMemberTotalTeamNumber(params);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("memberId", params.get("id"));
		Member member = getMemberInfo(map);
		dto.setData(member);
		return dto;
	}

	@Override
	public Dto<Member> login(String cellphone, String password) {
		Dto<Member> dto = new Dto<Member>();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("cellphone", cellphone);
		params.put("password", MD5Tools.encode(password));
		Member member = getMemberInfo(params);
		if (member == null) {
			dto.errorMsg("账号密码不正确");
			return dto;
		}
		if (StringUtils.equals(member.getStatus(), "1")) {
			dto.errorMsg("您的账号号已被管理员禁用");
			return dto;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("memberId", member.getMemberId());
		member = getMemberInfo(map);
		dto.setData(member);
		return dto;
	}

	@Override
	public Dto<Object> forgetPassword(String cellphone, String password,
			String verifyCode) {
		Dto<Object> dto = new Dto<Object>();
		if (memberMapper.checkIsExistCellphone(cellphone) <= 0) {
			dto.errorMsg("该手机号不存在,请重新输入");
			return dto;
		}
		ValueWrapper vc = cacheManager.getCache("verifyCodeCache").get(
				cellphone + "fpVerifyCode");
		if (vc == null || !StringUtils.equals(vc.get().toString(), verifyCode)) {
			dto.errorMsg("您的验证码已过期或不正确，请重新获取");
			return dto;
		}
		memberMapper.forgetPassword(MD5Tools.encode(password), cellphone);
		return dto;
	}

	@Override
	public Dto<Member> findPersonDetail(String memberId) {
		Dto<Member> dto = new Dto<Member>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("memberId", memberId);
		Member member = getMemberInfo(map);
		dto.setData(member);
		return dto;
	}

	@Override
	public Dto<Member> updatePersonDetail(String memberId, String headImg,
			String nickName, String sex) {
		Dto<Member> dto = new Dto<Member>();
		memberMapper.updatePersonDetail(headImg, nickName, sex, memberId);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("memberId", memberId);
		Member member = getMemberInfo(map);
		dto.setData(member);
		return dto;
	}

	@Override
	public ListDto<BankCard> findBankCardList(String memberId) {
		ListDto<BankCard> dto = new ListDto<BankCard>();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("memberId", memberId);
		List<BankCard> bankCardList = memberMapper.findBankCardList(params);
		dto.setList(bankCardList);
		return dto;
	}

	@Override
	public Dto<Object> addBankCard(String memberId, String bankCardNumber,
			String reservedName, String bankName, String bankBranch) {
		Dto<Object> dto = new Dto<Object>();
		memberMapper.insertBankCard(memberId, bankCardNumber, reservedName,
				bankName, bankBranch);
		return dto;
	}

	@Override
	public Dto<Object> deleteBankCard(String memberId, String bankCardId) {
		Dto<Object> dto = new Dto<Object>();
		memberMapper.deleteBankCard(bankCardId);
		return dto;
	}

	@Override
	public Dto<Object> applyWithdrawals(String memberId, String bankCardId,
			String money, String brokerage, String payMoney) {
		Dto<Object> dto = new Dto<Object>();
		BankCard bankCard = memberMapper.findBankCardInfo(bankCardId);
		memberMapper.insertWithdrawals(memberId, bankCard.getBankCardNumber(),
				bankCard.getReservedName(), bankCard.getBankName(),
				bankCard.getBankBranch(), money, brokerage, payMoney);
		return dto;
	}

	@Override
	public ListDto<Withdrawals> findWithdrawalsList(String memberId) {
		ListDto<Withdrawals> dto = new ListDto<Withdrawals>();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("memberId", memberId);
		List<Withdrawals> withdrawalsList = memberMapper
				.findWithdrawalsList(params);
		dto.setList(withdrawalsList);
		return dto;
	}

	@Override
	public Dto<Object> applyIdentification(String memberId, String name,
			String idCardNumber, String idCardImg) {
		Dto<Object> dto = new Dto<Object>();
		memberMapper.insertIdentification(memberId, name, idCardNumber,
				idCardImg);
		return dto;
	}

	@Override
	public ListDto<Team> findTeamList(String memberId, String inviteCode) {
		ListDto<Team> dto = new ListDto<Team>();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("memberId", memberId);
		params.put("inviteCode", inviteCode);
		List<Team> teamList = memberMapper.findTeamList(params);
		dto.setList(teamList);
		return dto;
	}
}
